package com.lpwa.system.service.impl;

import java.util.List;

import com.lpwa.common.exception.BusinessException;
import com.lpwa.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.lpwa.system.mapper.DeviceModelMapper;
import com.lpwa.system.domain.DeviceModel;
import com.lpwa.system.service.IDeviceModelService;
import com.lpwa.common.core.text.Convert;

/**
 * 设备型号 服务层实现
 * 
 * @author tanmu
 * @date 2019-06-04
 */
@Service
public class DeviceModelServiceImpl implements IDeviceModelService 
{
	@Autowired
	private DeviceModelMapper deviceModelMapper;

	/**
     * 查询设备型号信息
     * 
     * @param modelId 设备型号ID
     * @return 设备型号信息
     */
    @Override
	public DeviceModel selectDeviceModelById(Long modelId)
	{
	    return deviceModelMapper.selectDeviceModelById(modelId);
	}
	/**
	 * 查询设备型号列表
	 *
	 * @param modelCode 设备型号代号code
	 * @return 设备型号集合
	 */
	@Override
	public DeviceModel selectDeviceModelByCode(String modelCode) {
		return  deviceModelMapper.selectDeviceModelByCode(modelCode);
	}

	/**
     * 查询设备型号列表
     * 
     * @param deviceModel 设备型号信息
     * @return 设备型号集合
     */
	@Override
	public List<DeviceModel> selectDeviceModelList(DeviceModel deviceModel)
	{
	    return deviceModelMapper.selectDeviceModelList(deviceModel);
	}
	
    /**
     * 新增设备型号
     * 
     * @param deviceModel 设备型号信息
     * @return 结果
     */
	@Override
	public int insertDeviceModel(DeviceModel deviceModel)
	{
	    return deviceModelMapper.insertDeviceModel(deviceModel);
	}
	
	/**
     * 修改设备型号
     * 
     * @param deviceModel 设备型号信息
     * @return 结果
     */
	@Override
	public int updateDeviceModel(DeviceModel deviceModel)
	{
	    return deviceModelMapper.updateDeviceModel(deviceModel);
	}

	/**
     * 删除设备型号对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
	@Override
	public int deleteDeviceModelByIds(String ids)
	{
		return deviceModelMapper.deleteDeviceModelByIds(Convert.toStrArray(ids));
	}
	/**
	 * 导入设备型号数据
	 *
	 * @param modelList 人员数据列表
	 * @param isUpdateSupport 是否更新支持，如果已存在，则进行更新数据
	 * @param operName 操作用户
	 * @return 结果
	 */
	@Override
	public String importData(List<DeviceModel> modelList, Boolean isUpdateSupport, String operName) {
		if (StringUtils.isNull(modelList) || modelList.size() == 0)
		{
			throw new BusinessException("导入设备型号数据不能为空！");
		}
		int successNum = 0;
		int failureNum = 0;
		StringBuilder successMsg = new StringBuilder();
		StringBuilder failureMsg = new StringBuilder();
//		String password = configService.selectConfigByKey("sys.person.initPassword");
		for (DeviceModel model : modelList)
		{
			try
			{
				String code = model.getModelCode();
				if (!StringUtils.isEmpty(code)){
					//判斷code是否唯一
					if (!checkLableIdUnique(code)){
						failureNum++;
						String msg = "<br/>" + failureNum + "、型号 " + model.getModelNumber() + " 导入失败：";
						failureMsg.append(msg + "代号'"+code+"'已存在");
						continue;
					}
				}
				String deviceTypeName = model.getDeviceTypeName();
//				"0=标签,1=基站,2=其他设备"
				if (deviceTypeName.equals("标签")){
					model.setDeviceType(0);
				}else if (deviceTypeName.equals("基站")){
					model.setDeviceType(1);
				}else{
					model.setDeviceType(2);
				}
				model.setCreateBy(operName);
				insertDeviceModel(model);
			}
			catch (Exception e)
			{
				failureNum++;
				String msg = "<br/>" + failureNum + "、型号 " + model.getModelNumber() + " 导入失败：";
				failureMsg.append(msg + e.getMessage());
//                log.error(msg, e);
			}
		}
		if (failureNum > 0)
		{
			failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
			throw new BusinessException(failureMsg.toString());
		}
		else
		{
			successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
		}
		return successMsg.toString();
//        return null;
	}

	@Override
	public Boolean checkLableIdUnique(String modelCode) {
		//判斷code是否唯一
		Integer count = deviceModelMapper.checkModelCodeUnion(modelCode);
		if (count>0){
			return false;
		}
		return true;
	}
}
